﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Spring.Core.IO;

namespace Agile.Engine.Utils
{
    public class VelocityUtils
    {
        static VelocityUtils()
        {
            engine = new NVelocity.App.VelocityEngine();
            engine.SetProperty("input.encoding", Encoding.UTF8.WebName);
            engine.SetProperty("output.encoding", Encoding.UTF8.WebName);
            engine.Init();
        }

        private static NVelocity.App.VelocityEngine engine;

        /// <summary>
        /// 合并velocity模板
        /// </summary>
        /// <param name="source">spring 资源定位</param>
        /// <param name="model">模型</param>
        /// <returns></returns>
        public static string Merge(Spring.Core.IO.IResource source, Hashtable model)
        {
            System.IO.StreamReader r = new System.IO.StreamReader(source.InputStream);
            var content = r.ReadToEnd();

            return MergeString(content, model);

        }

        /// <summary>
        /// 合并velocity模板泛型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">资源路径</param>
        /// <param name="func"></param>
        /// <param name="arg">需转换的参数</param>
        /// <returns></returns>
        public static string Merge<T>(string path, Func<T, Hashtable> func, T arg)
        {
            IResource resource = new Spring.Core.IO.ConfigurableResourceLoader().GetResource(path);
            return Merge(resource, func(arg));
        }

        /// <summary>
        /// 合并velocity模板
        /// </summary>
        /// <param name="path">资源路径</param>
        /// <param name="model">模型</param>
        /// <returns></returns>
        public static string Merge(string path, Hashtable model)
        {
            IResource resource = new Spring.Core.IO.ConfigurableResourceLoader().GetResource(path);
            return Merge(resource, model);
        }

        /// <summary>
        /// 合并文本内容
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="model">模型</param>
        /// <returns></returns>
        public static string MergeString(string content, Hashtable model)
        {
            var context = new NVelocity.VelocityContext(model);
            
            var writer = new System.IO.StringWriter();
            engine.Evaluate(context, writer, string.Empty, content);

            return writer.GetStringBuilder().ToString();
        }

        /// <summary>
        /// 使用任意对象合并资源
        /// </summary>
        /// <param name="content">需要合并的内容</param>
        /// <param name="key">对应vm模板中的对象名</param>
        /// <param name="model">模型实例</param>
        /// <returns></returns>
        public static string MergeString(string content, string key, object model)
        {
            var context = new NVelocity.VelocityContext();
            context.Put(key, model);

            var writer = new System.IO.StringWriter();
            engine.Evaluate(context, writer, string.Empty, content);

            return writer.GetStringBuilder().ToString();

        }

        /// <summary>
        /// 合并文本内容
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="content"></param>
        /// <param name="func"></param>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static string MergeString<T>(string content, Func<T, Hashtable> func, T arg)
        {
            return MergeString(content, func(arg));
        }
    }


}
